package com.xuelang.pipeline.app.task.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.xuelang.pipeline.app.task.TaskDefService;
import com.xuelang.pipeline.app.task.converter.TaskDefDTOToTaskDefConverter;
import com.xuelang.pipeline.client.common.constant.Constant;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.common.util.BizAssert;
import com.xuelang.pipeline.client.task.dto.TaskDefDTO;
import com.xuelang.pipeline.client.task.dto.TaskDefQuery;
import com.xuelang.pipeline.client.task.dto.TaskInfoFormDTO;
import com.xuelang.pipeline.client.task.dto.TaskTypeFormDTO;
import com.xuelang.pipeline.domain.task.TaskDef;
import com.xuelang.pipeline.domain.task.service.TaskDefDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 流水线任务定义服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class TaskDefServiceImpl implements TaskDefService {

    @Autowired
    TaskDefDomainService taskDefDomainService;

    @Override
    public PageRecord<TaskDefDTO> pageList(PageParam<TaskDefQuery> dtoPageParam) {
        TaskDefQuery condition = dtoPageParam.getCondition();
        condition = condition == null ? TaskDefQuery.builder().build() : condition;
        PageRecord<TaskDef> pageRecord = taskDefDomainService.pageList(
                dtoPageParam.getCurrentPage(),
                dtoPageParam.getPageSize(),
                condition);
        return new PageRecord<>(
                pageRecord.getCurrentPage(),
                pageRecord.getPageSize(),
                pageRecord.getTotalCount(),
                pageRecord.getTotalPages(),
                pageRecord.getRecords().stream().map(TaskDefDTOToTaskDefConverter.INSTANCE::taskDefToTaskDefDTO).collect(Collectors.toList()));
    }

    @Override
    public List<TaskDefDTO> list(TaskDefQuery taskDefQuery) {
        return taskDefDomainService.list(taskDefQuery).stream().map(TaskDefDTOToTaskDefConverter.INSTANCE::taskDefToTaskDefDTO).collect(Collectors.toList());
    }

    @Override
    public List<TaskTypeFormDTO> assemblyList(TaskDefQuery taskDefQuery) {
        List<TaskTypeFormDTO> taskTypeFormDTOList = new ArrayList<>();
        final List<TaskDefDTO> taskDefDTOList = taskDefDomainService.list(taskDefQuery).stream().map(TaskDefDTOToTaskDefConverter.INSTANCE::taskDefToTaskDefDTO).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(taskDefDTOList)) {
            Constant.TASK_TYPE_INFO_MAP.forEach((taskDefTypeKey, taskDefTypeName) -> {
                TaskTypeFormDTO taskTypeFormDTO = TaskTypeFormDTO.builder()
                        .typeName(taskDefTypeName)
                        .typeCode(taskDefTypeKey)
                        .build();
                List<TaskInfoFormDTO> taskInfoFormDTOList = new ArrayList<>();
                taskDefDTOList.stream().filter(taskDefDTO -> taskDefDTO.getTaskDefType().equals(taskDefTypeKey)).forEach(taskDefDTO -> taskInfoFormDTOList.add(
                        TaskInfoFormDTO.builder()
                                .taskId(taskDefDTO.getUuid())
                                .taskName(taskDefDTO.getName())
                                .taskCode(taskDefDTO.getCode())
                                .taskType(taskDefDTO.getTaskDefType())
                                .refTag(taskDefDTO.getRefTag())
                                .schema(taskDefDTO.getSchemaConfig())
                                .build()));
                taskTypeFormDTO.setTaskInfoList(taskInfoFormDTOList);
                taskTypeFormDTOList.add(taskTypeFormDTO);
            });

        }
        return taskTypeFormDTOList;
    }

    @Override
    public Boolean add(TaskDefDTO taskDefDTO) {
        BizAssert.notNull(taskDefDTO.getName(), "任务定义名称不能为空");
        BizAssert.notNull(taskDefDTO.getSchemaConfig(), "schemaConfig不能为空");
        BizAssert.notNull(taskDefDTO.getTaskDefType(), "任务定义类型不能为空");
        BizAssert.notNull(taskDefDTO.getRefTag(), "refTag不能为空");
        BizAssert.notNull(taskDefDTO.getCode(), "任务定义编码不能为空");
        TaskDef taskDef = TaskDefDTOToTaskDefConverter.INSTANCE.taskDefDTOToTaskDef(taskDefDTO);
        taskDef.setUuid(IdUtil.fastSimpleUUID());
        return taskDefDomainService.save(taskDef);
    }

    @Override
    public Boolean update(TaskDefDTO taskDefDTO) {
        BizAssert.notNull(taskDefDTO.getUuid(), "任务定义唯一标识不能为空");
        BizAssert.notNull(taskDefDTO.getSchemaConfig(), "schemaConfig不能为空");
        BizAssert.notNull(taskDefDTO.getTaskDefType(), "任务定义类型不能为空");
        BizAssert.notNull(taskDefDTO.getRefTag(), "refTag不能为空");
        BizAssert.notNull(taskDefDTO.getCode(), "任务定义编码不能为空");
        return taskDefDomainService.update(TaskDefDTOToTaskDefConverter.INSTANCE.taskDefDTOToTaskDef(taskDefDTO));
    }

    @Override
    public Boolean delete(String uuid) {
        BizAssert.notNull(uuid, "任务定义唯一标识不能为空");
        return taskDefDomainService.delete(uuid);
    }

    @Override
    public TaskDefDTO query(String uuid) {
        BizAssert.notNull(uuid, "任务定义唯一标识不能为空");
        return TaskDefDTOToTaskDefConverter.INSTANCE.taskDefToTaskDefDTO(taskDefDomainService.query(uuid));
    }

}
